package fr.esisar.cs420.swt;

import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;

import fr.esisar.cs420.traitement.Application;
import fr.esisar.cs420.traitement.Commande;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

import com.itextpdf.text.DocumentException;

public class AfficherCommandes extends Shell {
	private Table table;
	private Text zeroCommande;
	public static String element = null ;
	public List<String> listeCommande = new ArrayList<String>();


	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {

	}

	/**
	 * Create the shell.
	 * @param display
	 */
	public AfficherCommandes(Display display) {
		super(display, SWT.SHELL_TRIM);		
		setText("Vos Commandes");
		
		final TableViewer tableViewer = new TableViewer(this, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {

				org.eclipse.swt.graphics.Point p = new org.eclipse.swt.graphics.Point(e.x,e.y);
				
				element = (String) tableViewer.getCell(p).getElement() ;
			}
		});
		table.setBounds(10, 61, 572, 292);
		
		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setBounds(20, 40, 153, 15);
		lblNewLabel.setText("IdCommande");
		
		Label lblDateCommande = new Label(this, SWT.NONE);
		lblDateCommande.setText("Date Commande");
		lblDateCommande.setBounds(220, 40, 153, 15);
		
		Label lblPrixTotal = new Label(this, SWT.NONE);
		lblPrixTotal.setText("Prix Total ");
		lblPrixTotal.setBounds(486, 40, 153, 15);
		
		zeroCommande = new Text(this, SWT.NONE);
		zeroCommande.setEditable(false);
		zeroCommande.setBounds(116, 381, 359, 21);
		
		Button btnDtailDeLa = new Button(this, SWT.NONE);
		btnDtailDeLa.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				if( listeCommande.size() == 0  || element == null ){
					
				}
				else{
					try {
						Display display = Display.getDefault();
						AfficherArticlesCommande shell = new AfficherArticlesCommande(
								display);
						shell.open();
						shell.layout();
						while (!shell.isDisposed()) {
							if (!display.readAndDispatch()) {
								display.sleep();
							}
						}
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		btnDtailDeLa.setBounds(600, 122, 153, 25);
		btnDtailDeLa.setText("Détail de la commande");
		
		Button btnfacture = new Button(this, SWT.NONE);
		btnfacture.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				if(element != null ){
					StringTokenizer st = new StringTokenizer(element," ");
					try {
						Commande.obtenirFacture(Integer.valueOf(st.nextToken()), Application.getStmt());
					} catch (NumberFormatException e1) {
						e1.printStackTrace();
					} catch (FileNotFoundException e1) {
						e1.printStackTrace();
					} catch (DocumentException e1) {
						e1.printStackTrace();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		btnfacture.setBounds(600, 243, 153, 25);
		btnfacture.setText("Facture de la commande");
		createContents();

		tableViewer.setContentProvider( new ArrayContentProvider());
		tableViewer.setLabelProvider( new LabelProvider());
		listeCommande = Commande.afficherCommandes(Application.client);
		if( listeCommande.size() == 0 ){
			zeroCommande.setText("Vous n'avez pas encore effectué de commande");
		}
		else
			tableViewer.setInput(listeCommande);
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("Vos Commandes");
		setSize(788, 473);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
